// Copyright (c) 2021 Presto Labs Pte. Ltd.
// Author: fengyang

#pragma once

#include <iostream>
#include <memory>
#include <optional>
#include <set>
#include <sstream>
#include <string>
#include <vector>

#include "coin2/exchange/base/symbology/derivative_util.h"
#include "coin2/exchange/base/symbology/futures_product.h"
#include "coin2/exchange/dydx_futures/symbology/currency.h"

namespace coin2::exchange::dydx_futures::symbology {

using dydx_futures::symbology::DydxFuturesCurrency;

class DydxFuturesProduct
    : public base::symbology::FuturesProduct<DydxFuturesCurrency, DydxFuturesProduct>,
      public base::symbology::ProductFromStrMixin<DydxFuturesProduct> {
 public:
  using FuturesProduct = base::symbology::FuturesProduct<DydxFuturesCurrency, DydxFuturesProduct>;
  using ContractType = base::symbology::ContractType;
  using MarketType = base::market::MarketType;

 public:
  DydxFuturesProduct(
      const DydxFuturesCurrency& base,
      const DydxFuturesCurrency& quote,
      const std::string& native_product,
      const std::string& native_subscription_symbol,
      const ContractType contract_type,
      const int64_t expiry_timestamp,
      const int64_t resolve_timestamp)
      : FuturesProduct(
            base,
            quote,
            native_product,
            native_subscription_symbol,
            base::market::ExchangeType::Dydx,
            contract_type,
            expiry_timestamp,
            resolve_timestamp) {}

 public:
  static std::unique_ptr<DydxFuturesProduct> FromNativeStrImpl(
      const std::string& native_product,
      const int64_t resolve_timestamp) {
    using base::symbology::IsDateStr;

    if (!(native_product.size() >= 6 &&
          native_product.size() <= 11)) {
      return {};
    }

    std::string expiry_str = "PERPETUAL";
    std::unique_ptr<DydxFuturesCurrency> base;
    std::unique_ptr<DydxFuturesCurrency> quote;
    std::vector<std::string> splitted;
    splitted = Split(native_product, "-");
    CHECK_EQ(splitted.size(), 2);
    expiry_str = "PERPETUAL";
    base = DydxFuturesCurrency::FromNativeStrNoThrow(splitted[0], resolve_timestamp);
    quote = DydxFuturesCurrency::FromNativeStrNoThrow(splitted[1], resolve_timestamp);

    if (!base) return {};

    std::string native_product_tmp;
    std::string native_subscription_symbol;
    ContractType contract_type;
    int64_t expiry_timestamp;
    bool success = CalculateParams(
        *base,
        *quote,
        expiry_str,
        resolve_timestamp,
        &native_product_tmp,
        &native_subscription_symbol,
        &contract_type,
        &expiry_timestamp);
    if (success) {
      return std::make_unique<DydxFuturesProduct>(
          *base,
          *quote,
          native_product,
          native_subscription_symbol,
          contract_type,
          expiry_timestamp,
          resolve_timestamp);
    } else {
      return {};
    }
  }

  static bool CalculateParams(
      const DydxFuturesCurrency& base,
      const DydxFuturesCurrency& quote,
      const std::string& expiry_str,
      const int64_t resolve_timestamp,
      std::string* native_product,
      std::string* native_subscription_symbol,
      ContractType* contract_type,
      int64_t* expiry_timestamp) {
    using base::symbology::ContractType;
    using base::symbology::ExchangeType;
    using base::symbology::FindExpiryFriday;
    using base::symbology::IsDateStr;
    using base::symbology::StringToContractType;
    using base::symbology::ToDateStr;

    std::stringstream ss;

    // native_product
    auto type = StringToContractType(expiry_str);
    if (type) {
      *contract_type = type.value();
      // only support perpetual
      CHECK_EQ(*contract_type, ContractType::PERPETUAL);
      *expiry_timestamp = FuturesProduct::PERPETUAL_TIMESTAMP;
      ss << base.native_currency(MarketType::Futures) << "-"
          << quote.native_currency(MarketType::Futures);
    } else {
      return false;
    }
    *native_product = ss.str();
    *native_subscription_symbol = *native_product;

    return true;
  }
};

}  // namespace coin2::exchange::dydx_futures::symbology
